package org.third.java.jcf;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

class ExecutorServiceTask implements Callable<Boolean> {

	@Override
	public Boolean call() throws Exception {
		Boolean result = Boolean.TRUE;
		System.out.println("Executing the task: " + Thread.currentThread().getName());
		return result;
	}

}

public class ExectorServiceTest {

	public static void main(String[] args) {
		ExecutorService exectorService = null;
		try {
			exectorService = Executors.newFixedThreadPool(2);
			Set<ExecutorServiceTask> tasks = new HashSet<ExecutorServiceTask>();
			tasks.add(new ExecutorServiceTask());
			tasks.add(new ExecutorServiceTask());
			tasks.add(new ExecutorServiceTask());
			tasks.add(new ExecutorServiceTask());
			System.out.println("all task is prepared to start");
//			exectorService.invokeAll(tasks);
			exectorService.invokeAll(tasks, 10, TimeUnit.MINUTES);
			System.out.println("all the tasks finished");
		} catch (Exception e) {
			exectorService.shutdown();
		}

	}
}
